//主界面
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shensu/page/allgame_viewpage.dart';
import 'package:flutter_shensu/page/my_page.dart';
import 'package:flutter_shensu/page/myispeed_page.dart';

class NewMainPage extends StatefulWidget {
  static final String sName = "newmain";

  @override
  _MainPageState createState() => new _MainPageState();
}

class _MainPageState extends State<NewMainPage> {
  final appbarTitles = ['全部游戏', '加速', '我的'];

  final tabTextStyleSelected = new TextStyle(color: const Color(0xFF00DCC8));
  final tabTextStyleNormal = new TextStyle(color: const Color(0xff969696));

  var tabImages;
  var _body;
  var pages;

  int _tabIndex = 1;

  @override
  void initState() {
    super.initState();

    pages = <Widget>[
      AllGameViewPage(),
      MyISpeedPage(),
      MyPage(),
    ];

    if (tabImages == null) {
      tabImages = [
        [
          getTabImage('static/images/ic_all_game_normal.png'),
          getTabImage('static/images/ic_all_game_selected.png')
        ],
        [
          getTabImage('static/images/ic_boost_normal.png'),
          getTabImage('static/images/ic_boost_selected.png')
        ],
        [
          getTabImage('static/images/ic_my_normal.png'),
          getTabImage('static/images/ic_my_selected.png')
        ]
      ];
    }
  }

  @override
  Widget build(BuildContext context) {
    _body = IndexedStack(
      children: pages,
      index: _tabIndex,
    );

    return Scaffold(
      body: _body,
      bottomNavigationBar: _getBottomNavigationBar(),
    );
  }

  Widget _getBottomNavigationBar() {
    return CupertinoTabBar(
      backgroundColor: Colors.white,
      items: <BottomNavigationBarItem>[
        new BottomNavigationBarItem(icon: getTabIcon(0), title: getTabTitle(0)),
        new BottomNavigationBarItem(icon: getTabIcon(1), title: getTabTitle(1)),
        new BottomNavigationBarItem(icon: getTabIcon(2), title: getTabTitle(2)),
      ],
      currentIndex: _tabIndex,
      onTap: (index) {
        setState(() {
          _tabIndex = index;
        });
      },
    );
  }

  Image getTabIcon(int curIndex) {
    if (curIndex == _tabIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }

  Image getTabImage(path) {
    return new Image.asset(path, width: 20.0, height: 20.0);
  }

  Text getTabTitle(int curIndex) {
    return new Text(appbarTitles[curIndex], style: getTabTextStyle(curIndex));
  }

  TextStyle getTabTextStyle(int curIndex) {
    if (curIndex == _tabIndex) {
      return tabTextStyleSelected;
    }
    return tabTextStyleNormal;
  }
}
